package com.geek.novel.view.list

import android.content.Context
import android.database.DataSetObserver
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout

class AllListView: LinearLayout {

    private lateinit var mContext: Context

    //适配器
    private var adapter: BaseAdapter? = null

    constructor(context: Context) : super(context) {
        init(context)
    }
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context)
    }
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int = 0) : super(context, attrs, defStyleAttr) {
        init(context)
    }

    private fun init(context: Context){
        this.mContext = context

        //设置垂直布局
        this.orientation = VERTICAL
    }

    private fun notifyListChange(){
        //移除所有子项再重新添加，后面需要优化
        detachAllViewsFromParent()

        //刷新列表
        refreshList()
    }

    /**
     * 刷新列表
     */
    private fun refreshList(){
        val adapter = this.adapter ?: return
        val count = adapter.count
        for (i in 1..count){
            val position = i-1
            addItem(position)
        }
    }

    interface OnItemClickListener {
        fun onItemClick(parent: ViewGroup?, view: View?, position: Int, id: Long)
    }
    interface OnItemLongClickListener {
        fun onItemLongClick(parent: ViewGroup?, view: View?, position: Int, id: Long): Boolean
    }

    /**
     * 添加列表项目
     */
    private fun addItem(position: Int): View?{
        val adapter = this.adapter ?: return null
        val view: View? = adapter.getView(position, null, this)
        if(view != null){
            this.bindEvent(view, position)
            val p = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
            this.addView(view, p)
        }
        return view
    }

    private fun bindEvent(view: View, position: Int){
        view.setOnClickListener{
            if(this.onItemClickListener != null){
                this.onItemClickListener!!.onItemClick(this, it, position, this.adapter!!.getItemId(position))
            }
        }
        view.setOnLongClickListener {
            if(this.onItemLongClickListener != null){
                this.onItemLongClickListener!!.onItemLongClick(this, it, position, this.adapter!!.getItemId(position))
            }else{
                false
            }
        }
    }

    private var onItemClickListener: OnItemClickListener? = null
    private var onItemLongClickListener: OnItemLongClickListener? = null

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?){
        this.onItemClickListener = onItemClickListener
    }
    fun setOnItemLongClickListener(onItemLongClickListener: OnItemLongClickListener?){
        this.onItemLongClickListener = onItemLongClickListener
    }


    fun getAdapter(): BaseAdapter? {
        return this.adapter
    }

    fun setAdapter(adapter: BaseAdapter) {
        this.adapter = adapter

        adapter.registerDataSetObserver(object : DataSetObserver() {
            override fun onChanged() {
                notifyListChange()
            }
            override fun onInvalidated() {
                notifyListChange()
            }
        })
    }

}